from flask import Flask, Blueprint, redirect, render_template, request
from Stu import Student
students = []
stu = Blueprint('stu', __name__, url_prefix='/stu')
@stu.route("/index")
def index():
    return render_template("index.html",students=students)
@stu.route("/add", methods=['GET',"POST"])
def add():
    if request.method == "GET":
        return render_template("add.html")
    else:
        name = request.values.get("name")
        age = int(request.values.get("age"))
        gender = request.values.get("gender")
        address = request.values.get("address")

        obj = Student(name=name, age=age, gender=gender, address=address)

        students.append(obj)
        return redirect("/stu/index")
@stu.route("/change/<sid>", methods=['GET',"POST"])
def change(sid):
    if request.method == "GET":
        cstu = ""
        for stu in students:
            if stu.sid == int(sid):
                cstu = stu
                print(cstu)
                break
        print(cstu)
        return render_template("change.html",stu=cstu)
    else:
        # 获取表单提交的数据
        name = request.values.get("name")
        age = int(request.values.get("age"))
        gender = request.values.get("gender")
        address = request.values.get("address")
        for stu in students:
            if stu.sid == int(sid):
                stu.name = name
                stu.age = age
                stu.gender = gender
                stu.address = address
                break
        return redirect("/stu/index")

@stu.route("/del/<sid>")
def delete(sid):
    for stu in students:
        if stu.sid == int(sid):
            students.remove(stu)
            break
    return redirect("/stu/index")